package cn.edu.scau.dormitory_management_system.module.examine.controller;

import cn.edu.scau.dormitory_management_system.aop.annotation.MarkDown;
import cn.edu.scau.dormitory_management_system.constant.Consts;
import cn.edu.scau.dormitory_management_system.module.examine.entity.DormAdjustAuditEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.VO.DormAdjustAuditVO;
import cn.edu.scau.dormitory_management_system.module.examine.service.*;
import cn.edu.scau.dormitory_management_system.entity.UserEntity;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ：Vizzk
 * @description：TODO
 * @date ：2021/8/19 14:09
 */
@RestController
@RequestMapping("/admin/dormAdjust/audit")
public class DormAdjustAuditController {
    @Autowired
    DormAdjustApplyService dormAdjustApplyService;
    @Autowired
    DormAdjustAuditService dormAdjustAuditService;

    @PostMapping("/insertDeptAudit")
    public Object insertDeptAudit(HttpServletRequest request, @RequestBody DormAdjustAuditEntity auditEntity){
        JSONObject jsonObject = new JSONObject();

        UserEntity user = (UserEntity) request.getAttribute("admin");
        auditEntity.setUserId(user.getUserId());
        auditEntity.setUserName(user.getUserName());
        try {
            dormAdjustApplyService.insertDeptAudit(auditEntity);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "审核完成");
        }
        catch (Exception e){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "提交失败");
        }
        return jsonObject;
    }

    @MarkDown
    @PostMapping("/insertCollegeAudit")
    public Object insertAdjustCollegeAudit(HttpServletRequest request, @RequestBody DormAdjustAuditEntity auditEntity){
        JSONObject jsonObject = new JSONObject();

        UserEntity user = (UserEntity) request.getAttribute("admin");
        auditEntity.setUserId(user.getUserId());
        auditEntity.setUserName(user.getUserName());
        try {
            dormAdjustApplyService.insertCollegeAudit(auditEntity);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "审核完成");
        }
        catch (Exception e){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "提交失败");
        }
        return jsonObject;
    }
    @GetMapping("/listAuditsByAdjustId")
    public Object listAuditsByDormAdjustId(Integer adjustId){
        JSONObject jsonObject = new JSONObject();
        try {
            //List<DormAdjustAuditEntity> audits = dormAdjustAuditService.listAuditsByDormAdjustId(adjustId);
            List<DormAdjustAuditVO> audits = dormAdjustAuditService.listAuditVOByDormAdjustId(adjustId);
            jsonObject.put(Consts.CODE, 200);
            jsonObject.put(Consts.MSG, "查询成功");
            jsonObject.put("audits",audits);
        }
        catch (Exception e){
            e.printStackTrace();
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "查询失败");
        }
        return jsonObject;
    }
}
